/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package corba;

import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 *
 * @author Paul
 */
public class CorbaConnection {
    
    private Context context;
    private String hostname;
    private int port;

    public CorbaConnection(String hostname, int port) {
        this.hostname = hostname;
        this.port = port;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
    
    public void connect() throws NamingException
    {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
        env.put(Context.PROVIDER_URL, "iiop://"+this.hostname+":"+this.port);
        
        context = new  InitialContext(env);
    }
    public Object getRemoteObject(String name, Class<?> clazz)
    {
        try {
            Object objRef = context.lookup(name);
            return PortableRemoteObject.narrow(objRef,clazz);
        } catch (NamingException ex) {
            Logger.getLogger(CorbaConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
        
    }
}
